//
//  ByHomeAdvertView.m
//  hutouben_ios
//
//  Created by mc on 2017/7/17.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByHomeAdvertView.h"


@interface ByHomeAdvertView ()  <UIScrollViewDelegate>
{
    NSInteger _carouselAmount;
    CGFloat _duration;
}

@property (nonatomic, strong) UIScrollView *scrollView;             // 滚动视图
@property (nonatomic, strong) UIPageControl *pageControl;           // 分页视图
@property (nonatomic, strong) NSTimer *timer;                       // 定时器
@end

@implementation ByHomeAdvertView

- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}

- (instancetype)initWithFrame:(CGRect)frame
                timerDuration:(CGFloat)duration {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(_timerAction:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        
    }
    
    return self;
}

#pragma mark -- 创建视图 --
// 创建滚动视图
- (void)_initScrollView {
    
    if (_scrollView) {
        return;
    }
    
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(40, 10 + 15 + 5, 30, 30)];
    leftImg.image = [UIImage imageNamed:@"Index_hot"];
    [self addSubview:leftImg];
    
    CGFloat width = [@"公司热点" getSizeOfString:16].width;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(leftImg.right + 5, 15 + 15 + 5, width, 20)];
    label.font = FONT(16);
    label.textColor = [UIColor whiteColor];
    label.text = @"公司热点";
    [self addSubview:label];
    
    CGFloat x = label.right + 10;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(label.right + 10, 15, self.width - x - 10, self.height - 20)];
    _scrollView.contentSize = CGSizeMake(_scrollView.width, _scrollView.height* (_carouselAmount + 2));
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    _scrollView.layer.cornerRadius = 4;
    _scrollView.delegate = self;
    
    // 为了实现轮播，在需要显示的三个视图前后个在放一个（在第一个视图前面方最后一个视图，在最后一个视图后面方第一个视图）
    // 中间主要显示的视图
    for (int i = 0; i < _carouselAmount + 2; i ++) {
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, i * _scrollView.height, _scrollView.width - 20,  _scrollView.height)];
        contentLabel.numberOfLines = 2;
        contentLabel.textAlignment = NSTextAlignmentCenter;
        contentLabel.font = FONT(14);
        contentLabel.tag = 100 + i;
        contentLabel.textColor = [UIColor whiteColor];
        [_scrollView addSubview:contentLabel];
 
    }
    
    // 显示第一张图片
    _scrollView.contentOffset = CGPointMake(0, _scrollView.height);
    
    [self insertSubview:_scrollView atIndex:0];
    
}

#pragma mark -- 滑动视图代理 --
// 滑动视图已经停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //如果已经滑动到最后一张，则跳到第二张
    if (_scrollView.contentOffset.y == (_carouselAmount + 1) * _scrollView.height) {
        _scrollView.contentOffset = CGPointMake(0, _scrollView.height);
    }
    //如果滑动到到第一张，则跳到倒数第二张
    else if (_scrollView.contentOffset.y == _scrollView.height) {
        _scrollView.contentOffset = CGPointMake(0,_carouselAmount * _scrollView.height);
    }
    
    // 设置分页视图的当前页数显示
    NSInteger curPage = _scrollView.contentOffset.y / _scrollView.height - 1;
    _pageControl.currentPage = curPage;
}


#pragma mark -- 定时器方法 --
// 定时器方法
- (void)_timerAction:(NSTimer *)timer {
    // 跳转到下一页
    [UIView animateWithDuration:0.5 animations:^{
        _scrollView.contentOffset = CGPointMake(0, _scrollView.contentOffset.y + _scrollView.height);
    }];
    //如果已经滑动到最后一张，则跳到第二张
    if (_scrollView.contentOffset.y == (_carouselAmount + 1) * _scrollView.height) {
        _scrollView.contentOffset = CGPointMake( 0, _scrollView.height);
    }
    //如果滑动到到第一张，则跳到倒数第二张
    else if (_scrollView.contentOffset.y == 0 ) {
        _scrollView.contentOffset = CGPointMake( 0,_scrollView.height * _carouselAmount);
    }
    
}


-(void)setAdvertAry:(NSArray *)advertAry{
    NSLog(@"%@",advertAry);
    // 如果没有轮播，则不轮播
    if (advertAry == nil || advertAry.count == 0) {
//        ByModelIndexHot *model = [[ByModelIndexHot alloc] init];
        NSString *str = @"";
        advertAry = @[str];
    }
    
    NSMutableArray *tmpAry = [NSMutableArray array];
    tmpAry = [advertAry mutableCopy];
    [tmpAry addObject:advertAry[0]];
    [tmpAry insertObject:advertAry[advertAry.count - 1] atIndex:0];
    
    // 设置好轮播数组
    _advertAry = [tmpAry copy];
    
    // 设置轮播数量
    _carouselAmount = advertAry.count;
    
    // 创建滑动视图
    [self _initScrollView];
    
    // 如果只有一张图片，则不轮播
    if (_carouselAmount == 1) {
        _scrollView.scrollEnabled = NO;
        [_timer invalidate];
        _timer = nil;
    }
    
    // 给轮播图片视图设置从网上获取的图片
    for (int tag = 100; tag < 100 + _carouselAmount + 2; tag ++) {
        UILabel *contentLabel = [_scrollView viewWithTag:tag];
        contentLabel.text = _advertAry[tag - 100];
    }

}

//- (void)clickAtIndex:(UITapGestureRecognizer *)tap {
//    NSLog(@"点击");
//    NSInteger index = tap.view.tag - 100; // index对应于数据数组中的元素索引
//    ByNetTopPostModel *model = _advertAry[index];
//    if (self.itemDidTap) {
//        self.itemDidTap(model.post_id.integerValue);
//    }
//}


@end
